/**
 * MTranslateAnimation.java
 * classes: com.hymobile.common.animation.MTranslateAnimation
 * 梁天刚 created at 2011-12-8 下午2:44:02
 */
package com.wzz.common.animation;

import android.view.animation.Transformation;

/**
 * com.hymobile.common.animation.MTranslateAnimation
 * 
 * @author 梁天刚
 * 
 *         created at 2011-12-8 下午2:44:02
 */
public class MTranslateAnimation extends MAnimation {
  private int mFromXType = ABSOLUTE;
  private int mToXType = ABSOLUTE;

  private int mFromYType = ABSOLUTE;
  private int mToYType = ABSOLUTE;

  private float mFromXValue = 0.0f;
  private float mToXValue = 0.0f;

  private float mFromYValue = 0.0f;
  private float mToYValue = 0.0f;

  private float mFromXDelta;
  private float mToXDelta;
  private float mFromYDelta;
  private float mToYDelta;

  public MTranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta) {
    mFromXValue = fromXDelta;
    mToXValue = toXDelta;
    mFromYValue = fromYDelta;
    mToYValue = toYDelta;

    mFromXType = ABSOLUTE;
    mToXType = ABSOLUTE;
    mFromYType = ABSOLUTE;
    mToYType = ABSOLUTE;
  }

  /**
   * Constructor to use when building a TranslateAnimation from code
   * 
   * @param fromXType
   *          Specifies how fromXValue should be interpreted. One of
   *          Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or
   *          Animation.RELATIVE_TO_PARENT.
   * @param fromXValue
   *          Change in X coordinate to apply at the start of the animation.
   *          This value can either be an absolute number if fromXType is
   *          ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
   * @param toXType
   *          Specifies how toXValue should be interpreted. One of
   *          Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or
   *          Animation.RELATIVE_TO_PARENT.
   * @param toXValue
   *          Change in X coordinate to apply at the end of the animation. This
   *          value can either be an absolute number if toXType is ABSOLUTE, or
   *          a percentage (where 1.0 is 100%) otherwise.
   * @param fromYType
   *          Specifies how fromYValue should be interpreted. One of
   *          Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or
   *          Animation.RELATIVE_TO_PARENT.
   * @param fromYValue
   *          Change in Y coordinate to apply at the start of the animation.
   *          This value can either be an absolute number if fromYType is
   *          ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
   * @param toYType
   *          Specifies how toYValue should be interpreted. One of
   *          Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or
   *          Animation.RELATIVE_TO_PARENT.
   * @param toYValue
   *          Change in Y coordinate to apply at the end of the animation. This
   *          value can either be an absolute number if toYType is ABSOLUTE, or
   *          a percentage (where 1.0 is 100%) otherwise.
   */
  public MTranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue) {

    mFromXValue = fromXValue;
    mToXValue = toXValue;
    mFromYValue = fromYValue;
    mToYValue = toYValue;

    mFromXType = fromXType;
    mToXType = toXType;
    mFromYType = fromYType;
    mToYType = toYType;
  }

  @Override
  protected void applyTransformation(float interpolatedTime, Transformation t) {
    float dx = mFromXDelta;
    float dy = mFromYDelta;
    if (mFromXDelta != mToXDelta) {
      dx = mFromXDelta + ((mToXDelta - mFromXDelta) * interpolatedTime);
    }
    if (mFromYDelta != mToYDelta) {
      dy = mFromYDelta + ((mToYDelta - mFromYDelta) * interpolatedTime);
    }
    t.getMatrix().setTranslate(dx, dy);
  }

  @Override
  public void initialize(int width, int height, int parentWidth, int parentHeight) {
    super.initialize(width, height, parentWidth, parentHeight);
    mFromXDelta = resolveXSize(mFromXType, mFromXValue, width, parentWidth);
    mToXDelta = resolveXSize(mToXType, mToXValue, width, parentWidth);
    mFromYDelta = resolveYSize(mFromYType, mFromYValue, height, parentHeight);
    mToYDelta = resolveYSize(mToYType, mToYValue, height, parentHeight);
  }

}
